"""
构造程序逻辑
"""

import math

"""
找出所有水仙花数
水仙花数也被称为超完全数字不变数、自恋数、自幂数、阿姆斯特朗数，
它是一个3位数，该数字每个位上数字的立方之和正好等于它本身，
例如：$1^3 + 5^3+ 3^3=153$。
"""
for num in range(100, 1000):
    low = num % 10
    mid = num // 10 % 10
    high = num // 100
    if num == low ** 3 + mid ** 3 + high ** 3:
        print(num)
print('\n')

"""
正整数的反转
"""
num = 153
reversed_num = 0
while num > 0:
    reversed_num = reversed_num * 10 + num % 10
    print("reversed_num:", reversed_num)
    num = num // 10
print(reversed_num)
print('\n')

"""
百钱百鸡
公鸡5元一只，母鸡3元一只，小鸡1元三只，用100块钱买一百只鸡，问公鸡、母鸡、小鸡各有多少只？
"""
for x in range(0, 20):
    for y in range(0, 33):
        z = 100 - x - y
        if 5 * x + 3 * y + z / 3 == 100:
            print('Roosters: %d, hens: %d, chicks: %d' % (x, y, z), end='\n')


# 斐波那契数列
def fibonacci(n):
    fib_list = [0, 1]
    for i in range(2, n + 1):
        fib_list.append(fib_list[i - 1] + fib_list[i - 2])
    return fib_list


# 输出
fib_sum = fibonacci(10)
print(fib_sum)


# 找出 10000 以内的完美数
def find_perfect_numbers(limit):
    perfect_numbers = []
    for number in range(1, limit + 1):
        factors = []
        for i in range(1, number):
            if number % i == 0:
                factors.append(i)
        if sum(factors) == number:
            perfect_numbers.append(number)
    return perfect_numbers


print('The perfect number within 10000 is', find_perfect_numbers(10000))


# 找出 100 以内所有的素数
def find_prime_numbers(limit):
    prime_numbers = []
    for number in range(2, limit + 1):
        is_prime = True
        end = int(math.sqrt(number))
        for i in range(2, end + 1):
            if number % i == 0:
                is_prime = False
                break
        if is_prime:
            prime_numbers.append(number)
    return prime_numbers


print('The prime number within 100 is ', find_prime_numbers(100))